package mc.contents.web.listener;
import mc.contents.pojo.view.ReshelfDisplay;
import mc.contents.pojo.view.ReshelfItem;
import mc.contents.service.ICrawService;
import mc.contents.web.common.*;
import mc.contents.web.request.ReshelfCrawRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;

/**
 * Created by Administrator on 14-12-8.
 */
public class ReshelfAppAsyncListener extends AbstractAsyncListener {
    private static Logger log = LoggerFactory.getLogger( ReshelfAppAsyncListener.class );
    private ICrawService crawService;

    public ReshelfAppAsyncListener( Long id, Long start,ICrawService crawService, String name ) {
        super( id, start, name );
        this.crawService = crawService;
    }

    @Override
    public String handleResult( String errMsg,CommonContext commonContext,long start ) {
        List<CrawResult> results = commonContext.getCrawResults();
        ReshelfCrawRequest crawRequest = (ReshelfCrawRequest) commonContext.getCrawRequest();
        Set<ReshelfItem> items = new HashSet<>(  );
        if( results != null && !results.isEmpty() ){
            items = crawService.getReshelfItems( crawRequest,results );
        }
        ReshelfDisplay display = new ReshelfDisplay();
        display.setItems( items );
        display.setDuration( System.currentTimeMillis() - start );
        return getJsonStr( display, errMsg);
    }

}
